const http = require("http")
const fs = require("fs")
const cheerio = require("cheerio")
const path = require('path')
const superAgent = require('superagent')
const superCharset = require('superagent-charset')
const GetImages = require('./getImages')
const async = require('async')
superCharset(superAgent)
const MoviesModel = require('./models/movies')
// var urlList = JSON.parse(fs.readFileSync('list.json', 'utf8'))
module.exports = {
  go (movie, count, cb2) {
    superAgent
    .get(movie.link)
    .set('Connection', 'keep-alive')
    .timeout({
      response: 10000 // Wait 10 seconds for the server to start sending,
    })
    .set('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8')
    .set('User-Agent', 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36')
    .charset('gb2312')
    .end((err, res) => {
      if (err || !res.ok) {
         console.log('电影：' + movie.title +'，出错了')
         cb2(false, 'done')
         return
      } else {
        var $ = cheerio.load(res.text, {
            decodeEntities: false
        })
        async.waterfall([
          (cb3) => {
            try {
              let title = movie.title
              // 解析图片，存到本地
              let $content = $('#endText');
              let cover = ''
              let withMap = ''
              let $img = $content.find('img')
              let imgs = []
              if ($img) {
                $img.each((index, img) => {
                  let imgUrl = $(img).attr('src')
                  let r = imgUrl.match(/(http:\/\/)?\w+\.(jpg|jpeg|gif|png)/) // 提取图片名
                  if (r && r.length > 0) {
                    imgs.push({
                      name: r[0],
                      url: imgUrl
                    })
                    $(img).attr('src', '/movie/' + r[0]) //替换成本地路径
                    if (index === 0) {
                      cover = '/movie/' + r[0];
                      imgs.push({
                        name: r[0],
                        url: imgUrl
                      })
                    }
                  }
                })
              }
              // 
              let downloadTdList = $content.find('table').find('td');
              let content = $content.html().replace(/<script.*<\/script>/g, '');
              // console.log(content)
              content = content.replace(/<center.*<\/center>/g, '');
              let name = content.match(/◎译\s+名.*<br>/);
              if (!name) {
                name = content.match(/◎片\s+名.*<br>/);
              }
              name = name ? name[0].replace(/◎译\s+名/g, '') : movie.title
              name = name.replace(/\s/g, '');
              name = name.replace(/<br>/g, '');
              name = name.replace(/<\/?[^>]*>/g,"");
              let nameArr = name.split('/');
              if (nameArr) {
                name = nameArr[0];
              }

              let pushDate = content.match(/◎年\s+代.*<br>/);
              pushDate = pushDate ? pushDate[0].replace(/◎年\s+代/, '') : '和平年代';
              pushDate = pushDate.replace(/\s/g, '');
              pushDate = pushDate.replace(/<br>/g, '');
              console.log('年代', pushDate);

              let country = content.match(/◎产\s+地.*<br>/);
              country = country ? country[0].replace(/<\/?[^>]*>/g,"") : '火星';
              country = country ? country.replace(/◎产\s+地/, '') : '火星';
              country = country.replace(/\s/g, '');
              country = country.replace(/<br>/g, '');

              let country2 = content.match(/◎国\s+家.*<br>/);
              countr2 = country2 ? country2[0].replace(/<\/?[^>]*>/g,"") : '火星';
              country2 = country2 ? country.replace(/◎国\s+家/, '') : '火星';
              country2 = country2.replace(/\s/g, '');
              country2 = country2.replace(/<br>/g, '');
              country = country || country2;
              console.log('产地', country);


              let movieType = content.match(/◎类\s+别.*<br>/);
              movieType = movieType ? movieType[0].replace(/<\/?[^>]*>/g, '') : '科幻';
              movieType = movieType ? movieType.replace(/◎类\s+别/, '') : '科幻';
              movieType = movieType.replace(/\s/g, '');
              movieType = movieType.replace(/<br>/g, '');
              console.log('类型', movieType);


              let language = content.match(/◎语\s+言.*<br>/);
              language = language ? language[0].replace(/◎语\s+言/g, '') : '火星';
              language = language.replace(/\s/g, '');
              language = language.replace(/<br>/g, '');
              console.log('语言', language);



              let subtitles = content.match(/◎字\s+幕.*<br>/);
              subtitles = subtitles ? subtitles[0].replace(/◎字\s+幕/g, '') : '火星语';
              subtitles = subtitles.replace(/\s/g, '');
              subtitles = subtitles.replace(/<br>/g, '');
              console.log('字幕', subtitles);


              let release = content.match(/◎上映日期.*<br>/);
              release = release ? release[0].replace(/<\/?[^>]*>/g, '') : '2019-01-01';
              release = release.match(/\d{2,4}-\d{1,2}-\d{0,2}/);
              release = release ? release[0]: '2019-01-01'
              console.log('上映日期', release);


              let duration = content.match(/◎片\s+长.*<br>/);
              duration = duration ? duration[0].replace(/◎片\s+长/g, '') : '130分钟';
              duration = duration.replace(/\s/g, '');
              duration = duration.replace(/<br>/g, '');
              console.log('片长', duration);
              

              let director = content.match(/◎导\s+演.*<br>/);
              director = director ? director[0].replace(/◎导\s+演/g, '') : '匿名';
              director = director.replace(/\s/g, '');
              director = director.replace(/<br>/g, '');
              console.log('导演', director);


              let starring = content.match(/◎主\s+演([\s|\S]*)◎简　　介/);
              let stars = [];
              if (starring) {
                stars = starring[1].split('<br>');
                stars = stars.map(star => {
                  star = star.replace(/\s/g, '');
                  star = star.replace(/<p>/g, '');
                  star = star.replace(/<\/p>/g, '');
                  return star;
                })
              }
              starring = stars.join('$')
              console.log('主演', starring);


              let download = '暂不提供下载';
              try {
                let td = downloadTdList[1];
                let a = $(td).find('a');
                download = $(td).find('a').attr('href');
              } catch(err) {}


              // content = content.replace(/IT之家/g, '火星');
              let score = content.match(/◎IMDb评分.[^\d]*(\d.?\d?).*user/);
              let score2 = content.match(/◎豆瓣评分.[^\d]*(\d.?\d?).*user/);
              score = score || score2;
              if (score) {
                score = score[1];
              } else {
                score = '5';
              }
              console.log('评分', score);
              content = content.replace(/[\s|\S]*<\!--Content Start-->/, '');
              content = content.replace(/【下载地址】[\s|\S]*/, '</p>');
              content = content.replace(/<span style="FONT-SIZE: 12px"><td>/, '');
              let ts = (new Date()).getTime();
              let sourceurl = movie.link;

              // desc
              let desc = content.match(/◎简\s+介([\s|\S]*)<p><img/);
              if (!desc) {
                desc = content.match(/◎简\s+介([\s|\S]*)<hr>/);
              }
              console.log(desc);
              desc = desc ? desc[0].replace(/◎简\s+介/, '') : content;
              desc = desc.replace(/<\/?[^>]*>/g, '');
              desc = desc.replace('<p><img', '');
              desc = desc.replace(/<p>/g, '');
              desc = desc.replace(/<img/g, '');
              desc = desc.replace(/<hr>/g, '');
              desc = desc.replace(/<\/p>/g, '');
              // cb3(null, [])
              MoviesModel.create({
                title: name,
                block: movie.block,
                content: desc,
                sourceurl,
                starring,
                pushDate,
                subtitles,
                release,
                name,
                cover,
                withMap,
                director,
                duration,
                movieType,
                score,
                download,
                ts,
                actors: '',
                dp_score: '',
                lan: language,
                made_in: country
              })
              .then(function (result) {
                console.log('找到电影第' + count + '部')
                cb3(false, imgs)
              })
              .catch((err) => {
                console.log(err);
                console.log('已存在该部电影：' + name)
                cb3(false, [])
              })
            } catch(err) {
              console.log(err);
              console.log('出错了：' + movie.link);
              cb3(false, [])
            }
          },
          (imgs, cb3) => { // 把图片存到本地库
            // cb3(null)
            if (imgs.length > 0) {
              async.eachSeries(imgs, (item, cb4) => {
                GetImages.go(item, 'movie', cb4)
              }, (res) => {
                console.log('完成一部')
                cb3(true, 'done')
              })
            } else {
              cb3(true, 'done')
            }
          }
        ], (res) => {
          cb2()
        })
      }
    })
  }
}
